// ==UserScript==
// @name         审核快捷键
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Ctrl+G通过，Q驳回，双击空格挂起，Alt+J通过并关闭
// @author       You
// @match        *://peisong.meituan.com/checkOrderCenter/taskInfo?id*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    // ==================== 配置区域 ====================
    const config = {
        // 单次按键配置
        singlePress: {
            'q': '驳回'
        },

        // 组合键配置
        comboKeys: {
            'ctrl+g': '通过',
            'alt+j': '通过并关闭'
        },

        // 多次按键配置
        multiPress: {
            'g': {
                text: '通过',
                times: 2,
                interval: 200
            },
            ' ': {
                text: '暂时挂起',
                times: 2,
                interval: 300
            }
        },

        // 高级设置
        settings: {
            toastDuration: 1500,
            // 提示显示时长(ms)
            maxHoldTime: 1000
            // 按键状态保持时间(ms)
        }
    };
    // ================================================

    // 按键状态记录器
    const keyRecords = {};

    // 主监听函数
    function initKeyListener() {
        document.addEventListener('keydown', (e) => {
            const key = e.key.toLowerCase();

            // 1. 优先检查组合键
            checkComboKeys(e);

            // 2. 检查多次按键
            checkMultiPress(key);

            // 3. 检查单次按键
            checkSinglePress(key, e);
        });
    }

    // 组合键检测
    function checkComboKeys(e) {
        Object.keys(config.comboKeys).forEach(combo => {
            const [modifier, key] = combo.split('+');
            if (e.key.toLowerCase() === key && e[`${modifier}Key`]) {
                triggerAction(config.comboKeys[combo]);
                e.preventDefault();
            }
        });
    }

    // 多次按键检测
    function checkMultiPress(key) {
        if (!config.multiPress[key]) return;

        const { text, times, interval } = config.multiPress[key];
        const now = Date.now();

        // 初始化记录
        if (!keyRecords[key]) {
            keyRecords[key] = {
                presses: [],
                lastTime: 0
            };
        }

        // 记录本次按键
        keyRecords[key].presses.push(now);
        keyRecords[key].lastTime = now;

        // 清理过期记录
        keyRecords[key].presses = keyRecords[key].presses.filter(
            time => now - time <= interval
        );

        // 检查触发条件
        if (keyRecords[key].presses.length >= times) {
            triggerAction(text);
            keyRecords[key].presses = [];
        }
    }

    // 单次按键检测
    function checkSinglePress(key, e) {
        if (config.singlePress[key]) {
            triggerAction(config.singlePress[key]);
            e.preventDefault();
        }
    }

    // 执行按钮点击
    function triggerAction(buttonText) {
        const btn = findButton(buttonText);
        if (btn) {
            btn.click();
            showToast(`✅ 已触发: ${buttonText}`);
        } else {
            console.warn('未找到按钮:', buttonText);
            showToast(`⚠️ 找不到按钮: ${buttonText}`, 3000);
        }
    }

    // 查找按钮函数
    function findButton(text) {
        const buttons = document.querySelectorAll('button');
        return Array.from(buttons).find(btn =>
            btn.innerText.trim() === text
        );
    }

    // 显示提示
    function showToast(message, duration = config.settings.toastDuration) {
        const toast = document.createElement('div');
        toast.style = `
            position: fixed;
            bottom: 20%;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(0,0,0,0.7);
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            z-index: 9999;
            font-size: 14px;
            animation: fadeIn 0.3s;
        `;
        toast.textContent = message;
        document.body.appendChild(toast);

        setTimeout(() => {
            toast.style.animation = 'fadeOut 0.5s';
            setTimeout(() => toast.remove(), 500);
        }, duration - 500);

        // 添加动画样式
        if (!document.getElementById('toast-style')) {
            const style = document.createElement('style');
            style.id = 'toast-style';
            style.textContent = `
                @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
                @keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } }
            `;
            document.head.appendChild(style);
        }
    }

    // 初始化清理定时器
    setInterval(() => {
        const now = Date.now();
        Object.keys(keyRecords).forEach(key => {
            if (now - keyRecords[key].lastTime > config.settings.maxHoldTime) {
                delete keyRecords[key];
            }
        });
    }, 2000);

    // 启动监听
    initKeyListener();
})();